// loadHtml
function loadHtml(){
    var argNum = arguments.length;
    var objId;                      //object which load the html as its innerHtml
    var url;                        //post target,
    var data;                       //post data, {key1:value1, key2:value2}

    if(argNum == 2)
    {
        objId=arguments[0];
        url=arguments[1];

        createWaitDiv();
        jQuery.post(url, {}, function(backdata){
                jQuery("#" + objId).html(backdata[0]);
                deleteWaitDiv();
            },
            "json");
    }
    else if(argNum == 3)
    {
        objId=arguments[0];
        url=arguments[1];
        data=arguments[2];

        createWaitDiv();
        jQuery.post(url, data, function(backdata){
            jQuery("#" + objId).html(backdata[0]);
            deleteWaitDiv();
        },
        "json");
    }
    else
    {
        //nums of arguments incorrect,error
        return false;
    }

    return true;
}

// loadHtml
function loadHeader(url, data, callback){
    jQuery("#divHeader").load(url , data, callback);
}

// loadHtml
function loadFooter(url, data, callback){
    jQuery("#divFooter" + objId).load(url , data, callback);
}

// getRequest
function getRequest(url, data, callback){
    jQuery.get(url , data, callback);
}

// getScript
function getScript(url, callback){
    jQuery.get(url, callback);
}

// getData
function getData(url, data, callback){
    jQuery.getJSON(url, callback);
}

// postRequest
function postRequest(){
    createWaitDiv();
    
    var argNum = arguments.length;
    var url;                        //post target
    var data;                       //post data, {key1:value1, key2:value2}
    var callback;                   //backData's deal function name, one argument required

    if(argNum == 1)
    {
        url=arguments[0];

        jQuery.post(url, {}, function(){deleteWaitDiv();});
    }
    else if(argNum == 2)
    {
        url=arguments[0];
        data=arguments[1];

        jQuery.post(url,data, function(){deleteWaitDiv();});
    }
    else if(argNum == 3)
    {
        url=arguments[0];
        data=arguments[1];
        callback=arguments[2];

        //set "json" as the default datatype
        jQuery.post(url, data, function(backData){ callback(backData); deleteWaitDiv(); }, "json");
    }
    else
    {
        //nums of arguments incorrect,error
        deleteWaitDiv();
        return false;
    }
    return true;
}

// ajaxRequest
function ajaxRequest(){
    createWaitDiv();
    var argNum = arguments.length;
    var postUrl;                    //post target
    var postData;                   //post data, {key1:value1, key2:value2}
    var successFun;                 //when success, deal function name, one arguments required
    var failFun;                    //when fail, deal function name

    if(argNum == 3)
    {
        postUrl=arguments[0];
        postData=arguments[1];
        successFun=arguments[2];
        failFun=ajaxFail;

        jQuery.ajax({
            type:"POST",
            url:postUrl,
            data:postData,
            success:function(backData,textStatus){
                if(textStatus == "success"){
                    successFun(backData);
                }else{
                    failFun(textStatus);
                };
                deleteWaitDiv();
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){ failFun(textStatus, errorThrown); deleteWaitDiv();},
            dataType:"json"
        });
    }
    else
    {
        //nums of arguments incorrect,error
        deleteWaitDiv();
        return false;
    }
    return true;
}

function ajaxFail(textStatus, errorThrown){
    //go to error page
    jQuery(document.createElement('form'))
    .attr('name', 'frmError')
    .attr('id', 'frmError')
    .attr('action',getHost() + 'default/error/ajax')
    .attr('method','post')
    .appendTo('body');

    jQuery(document.createElement('input'))
    .attr('name', "textStatus")
    .attr('id', "textStatus")
    .attr('type', "hidden")
    .attr('value', textStatus)
    .appendTo('#frmError');

    jQuery(document.createElement('input'))
    .attr('name', "errorThrown")
    .attr('id', "errorThrown")
    .attr('type', "hidden")
    .attr('value', errorThrown)
    .appendTo('#frmError');

    document.forms["frmError"].submit();

    //show error div
/*    jQuery(document.createElement('div'))
    .attr('id', 'divErrorPause')
    .css({ 'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '9999', 
        'backgroundColor': 'gray', 'opacity': '1', 
        'width': "100%", 
        'height': document.documentElement.scrollHeight + 'px',
        'text-align': 'center',
        'font': 'bold 24px arial'
        })
    .html("<br/><br/><br/><br/><br/>Fatal error occurs!<br/>Please contact system administrator")
    .appendTo('body'); 
*/}

function createWaitDiv(){
    jQuery(document.createElement('div'))
    .attr('id', 'divPause')
    .css({ 'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '9999999', 
        'width': "100%", 
        'height': document.documentElement.scrollHeight + 'px',
        'text-align': 'center',
        'font': 'bold 24px arial',
        'background-repeat': 'no-repeat',
        'background-position': 'center',
        'background-attachment': 'fixed'
        })
    .appendTo('body');
    setTimeout("displayWaitDiv()", 500);
}

function displayWaitDiv(){
    if(jQuery('#divPause') != null){
        jQuery('#divPause').css({'background-image':'url(' + getAjaxWaitImage() + ')'});
    }
}

function deleteWaitDiv(){
    setTimeout("jQuery('#divPause').replaceWith('');",0 );
}


function fileUpload(objId, urlAddress,callBack,uploadData){

    createWaitDiv();
    var failFun=ajaxFail;
    jQuery.ajaxFileUpload
    (
        {
            url:urlAddress,
            secureuri     : false,
            fileElementId : objId,
            data          :uploadData,
            dataType      : 'json',
            success       : function(backData){
                                callBack(backData);
                                deleteWaitDiv();
                            },
            error         : function(XMLHttpRequest, textStatus, errorThrown){
                                failFun(textStatus, errorThrown);
                                deleteWaitDiv();
                            }
        }
    )
    return true;
}